package com.wandian.news.mvvm.knowledge.scjs.scjsother;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.JsonObject;
import com.mylib.libcore.data.TTBaseAppApi;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.bean.KnowledgeJXSCBean;
import com.wandian.news.bean.KnowledgeTSBean;
import com.wandian.news.bean.KnowledgeTSDQBean;
import com.wandian.news.bean.KnowledgeYMSJBean;
import com.wandian.news.bean.KnowledgeYQBean;

import java.util.HashMap;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.AppConfig.TOUTIAO_KEY;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_SCJS_JXSC;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_SCJS_TS;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_SCJS_TSDQ;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_SCJS_YMSJ;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_SCJS_YQ;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description KnowledgeSCJSOtherViewModel
 **/
public class KnowledgeSCJSOtherViewModel extends BaseViewModel{
    private TTBaseAppApi baseAppApi;
    public MutableLiveData<List<KnowledgeTSDQBean.NewslistBean>> listTSDQ =  new MutableLiveData<>();
    public MutableLiveData<List<KnowledgeYQBean.NewslistBean>> listYQ =  new MutableLiveData<>();
    public MutableLiveData<List<KnowledgeTSBean.NewslistBean>> listTS =  new MutableLiveData<>();
    public MutableLiveData<List<KnowledgeJXSCBean.NewslistBean>> listJXSC =  new MutableLiveData<>();
    public MutableLiveData<List<KnowledgeYMSJBean.NewslistBean>> listYMSJ =  new MutableLiveData<>();
    public KnowledgeSCJSOtherViewModel(@NonNull Application application) {
        super(application);
        baseAppApi =  RetrofitUtil.getTouTiaoRetrofit().create(TTBaseAppApi.class);
    }


    public void getDataTitle(String title,String page){
        String url = "";
        switch (title){
            case "元曲":
                url = NEWS_KNOWLEDGE_SCJS_YQ;
                break;
            case "唐诗":
                url = NEWS_KNOWLEDGE_SCJS_TS;
                break;
            case "精选宋词":
                url = NEWS_KNOWLEDGE_SCJS_JXSC;
                break;
            case "优美诗句":
                url = NEWS_KNOWLEDGE_SCJS_YMSJ;
                break;
            case "唐诗大全":
                url = NEWS_KNOWLEDGE_SCJS_TSDQ;
                break;
        }
        if(UIUtils.isEmpty(url))return;
        showDialog();
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        hashMap.put("num","10");
        hashMap.put("page",page);
        String finalUrl = url;
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApi.getTouTiaoList(url,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Object o) {
                        dismissDialog();
                        Log.d("getTouTiaoList-sc",JsonUtil.toJson(o)+"");
                        if(!UIUtils.isEmpty(o)){
                            switch (finalUrl){
                                case NEWS_KNOWLEDGE_SCJS_TSDQ:
                                    KnowledgeTSDQBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),KnowledgeTSDQBean.class);
                                    if(!UIUtils.isEmpty(bean) && bean.getCode()==200 && bean.getNewslist().size()>0){
                                        listTSDQ.setValue(bean.getNewslist());
                                    }
                                    break;
                                case NEWS_KNOWLEDGE_SCJS_YQ:
                                    KnowledgeYQBean bean_yq = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),KnowledgeYQBean.class);
                                    if(!UIUtils.isEmpty(bean_yq) && bean_yq.getCode()==200 && bean_yq.getNewslist().size()>0){
                                        listYQ.setValue(bean_yq.getNewslist());
                                    }
                                    break;
                                case NEWS_KNOWLEDGE_SCJS_TS:
                                    KnowledgeTSBean bean_ts = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),KnowledgeTSBean.class);
                                    if(!UIUtils.isEmpty(bean_ts) && bean_ts.getCode()==200 && bean_ts.getNewslist().size()>0){
                                        listTS.setValue(bean_ts.getNewslist());
                                    }
                                    break;
                                case NEWS_KNOWLEDGE_SCJS_JXSC:
                                    KnowledgeJXSCBean bean_jxsc = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),KnowledgeJXSCBean.class);
                                    if(!UIUtils.isEmpty(bean_jxsc) && bean_jxsc.getCode()==200 && bean_jxsc.getNewslist().size()>0){
                                        listJXSC.setValue(bean_jxsc.getNewslist());
                                    }
                                    break;
                                case NEWS_KNOWLEDGE_SCJS_YMSJ:
                                    KnowledgeYMSJBean bean_ymsj = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),KnowledgeYMSJBean.class);
                                    if(!UIUtils.isEmpty(bean_ymsj) && bean_ymsj.getCode()==200 && bean_ymsj.getNewslist().size()>0){
                                        listYMSJ.setValue(bean_ymsj.getNewslist());
                                    }
                                    break;
                            }

                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                        ToastUtil.show("网络异常");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
